package edu.scnu.fans.fingertip.order.controller;

import edu.scnu.fans.fingertip.common.api.ApiResponse;
import edu.scnu.fans.fingertip.common.api.HeaderConstant;
import edu.scnu.fans.fingertip.common.api.ResponseUtils;
import edu.scnu.fans.fingertip.common.api.RowResponse;
import edu.scnu.fans.fingertip.order.input.CreateOrderInput;
import edu.scnu.fans.fingertip.order.input.OrderListInput;
import edu.scnu.fans.fingertip.order.output.OrderOutput;
import edu.scnu.fans.fingertip.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "订单API")
@RestController
public class OrderController {

    @Resource
    private OrderService orderService;

    @PostMapping("/fingertip/order/v1/create")
    @ApiOperation("创建订单")
    public ApiResponse<?> create(@RequestHeader(HeaderConstant.USER_ID) Long userId,
                                 @Valid @RequestBody CreateOrderInput input) {
        orderService.create(userId, input);
        return ResponseUtils.EMPTY;
    }

    @PostMapping("/fingertip/order/v1/list")
    @ApiOperation("订单列表")
    public RowResponse<List<OrderOutput>> list(@RequestHeader(HeaderConstant.USER_ID) Long userId,
                                               @Valid @RequestBody OrderListInput input) {
        List<OrderOutput> list = orderService.list(userId, input);
        return ResponseUtils.rows(list);
    }
}
